//
//  HWVistorView.swift
//  WeiBo
//
//  Created by macbook on 16/6/28.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit
import SnapKit

class HWVistorView: UIView {

    var colose:(()->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    private func setupUI(){
        
         backgroundColor = RGB(237, green: 237, blue: 237)
        //添加
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(houseImageView)
        addSubview(contentLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //设置圆圈的约束
        circleImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        //蒙版
        maskImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        //房子
        houseImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
        
        //文字
        contentLabel.snp_makeConstraints { (make) in
            
            make.width.equalTo(224)
            make.height.equalTo(36)
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(maskImageView)
        }
        
        //登录
        loginBtn.snp_makeConstraints { (make) in
            
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(16)
        }
        
        registerBtn.snp_makeConstraints { (make) in
            
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.right.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(16)
        }
        
        
    
}
    

    //懒加载创建控件
    //圆圈
    private lazy var circleImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //蒙版
    private lazy var maskImageView:UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    //房子
    private lazy var houseImageView:UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    
    //文字
    private lazy var contentLabel:UILabel = {
    
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textAlignment = .Center
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
    
        return label
    }()
    
    
    //登录
    private lazy var loginBtn:UIButton = {
    
        let btn = UIButton()
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
     btn.addTarget(self, action: #selector(HWVistorView.btnPresentController), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    //注册
    private lazy var registerBtn:UIButton = {
    
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.addTarget(self, action: #selector(HWVistorView.btnPresentController), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    
    //让圆圈旋转起来
    func circleRun(){
    
        let animate = CABasicAnimation(keyPath:"transform.rotation")
        
        //设置动画时间
        animate.duration = 15
        
        //设置旋转角度
        animate.toValue = 2 * M_PI
        
        //设置圈数
        animate.repeatCount = MAXFLOAT
        
        //当app推送到后台后关闭动画
        animate.removedOnCompletion = false
        
        //添加动画
        circleImageView.layer.addAnimation(animate, forKey: nil)
        
    }
    
    
    //外界调用的方法
    func setVistorView(text:String?,imageName:String?){
        
        //因为主页就就这个样子的，变化的只是图片和文字
        //所有就以这两个条件判断，外界调用的时候如果有值就代表不是主页
        if let ima = imageName,let tit = text {
        
            houseImageView.image = UIImage(named: ima)
            contentLabel.text = tit
             
            //别的页面没有圆圈
            circleImageView.hidden = true
        }else
        {
            //主页
           circleRun()
        }
    }
    
 
    
    //
    func btnPresentController(){
    
        colose?()
    }
    
    
}















